package com.example.tetris.logics.components;

import java.io.Serializable;
import java.util.*;

public class CellList implements Serializable {


    private List<Cell> cells;

    @Override
    public String toString() {
        return "CellList{" +
                "cells=" + cells +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        CellList cellList = (CellList) o;
        Set<Cell> cellSet = new HashSet<>(cellList.getCells());

        Set<Cell> originCellSet = new HashSet<>(cells);

        return Objects.equals(originCellSet,cellSet);
    }

    @Override
    public int hashCode() {
        return Objects.hash(cells);
    }

    public CellList() {
        cells = new ArrayList<>();
    }

    public void addCell(int x, int y) {
        cells.add(new Cell(x, y));
    }

    public void addCell(Cell cell){
        cells.add(cell);
    }

    /**
     * @param x
     * @param y
     * @return 是否成功删除
     */
    public Boolean deleteCell(int x, int y) {
        for (Cell cell : cells) {
            if (cell.getX() == x && cell.getY() == y) {
                cells.remove(cell);
                return true;
            }

        }
        return false;
    }

    /**
     *
     * @param x
     * @param y
     * @return 有则返回坐标为(x,y)的cell的副本,不存在则返回null
     */
    public Cell getCellAt(int x,int y){
        for(Cell cell : cells){
            if(x == cell.getX() && y == cell.getY())
                return cell.clone();
        }
        return null;
    }

    public List<Cell> getCells() {
        return cells;
    }



}
